package com.qixin.appPush;


import com.alibaba.fastjson.JSON;
import com.getui.push.v2.sdk.ApiHelper;
import com.getui.push.v2.sdk.GtApiConfiguration;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
import com.qixin.activiti.api.IActivitiService;
import com.qixin.modules.eaBasic.entity.EaTask;
import com.qixin.modules.eaBasic.mapper.EaTaskMapper;
import com.qixin.modules.eaBasic.vo.IdVo;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.EmptyUtil;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * app推送公用类
 */
@Service
public class AppPush {
    @Autowired
    private IActivitiService iActivitiService;
    @Autowired
    private ISysDictService sysDictService;
    @Autowired
    private EaTaskMapper eaTaskMapper;

    private static String appid = "";//RhpJmirZZs8dO7OiZir2c7
    private static String appkey = "";//t0g6kqya7h7IQwx219GhW5
    private static String masterSecret = "";//Z7WCyh0DWt7jOZOk4qFN09
    public static final String url = "https://restapi.getui.com/v2/";

    //推送信息
    public void pushMessage(String cid,String content, String title, String formId, String formType, String type, String status){
        //查询个推配置
        List<DictModel> list = sysDictService.queryDictItemsByCode("getui_set");
        if(list != null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                DictModel dictModel = list.get(i);
                String text = dictModel.getText();    //名称
                String value = dictModel.getValue();  //值
                if(text.equals("appid")){
                    appid = value;
                }
                if(text.equals("appkey")){
                    appkey = value;
                }
                if(text.equals("masterSecret")){
                    masterSecret = value;
                }
            }
        }

        // 设置httpClient最大连接数，当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
        System.setProperty("http.maxConnections", "200");
        GtApiConfiguration apiConfiguration = new GtApiConfiguration();
        //填写应用配置
        apiConfiguration.setAppId(appid);
        apiConfiguration.setAppKey(appkey);
        apiConfiguration.setMasterSecret(masterSecret);
        // 接口调用前缀，请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
        apiConfiguration.setDomain(url);
        // 实例化ApiHelper对象，用于创建接口对象
        ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
        // 创建对象，建议复用。目前有PushApi、StatisticApi、UserApi
        PushApi pushApi = apiHelper.creatApi(PushApi.class);

        sendMessage(pushApi,cid,content,title,formId,formType,type,status);
    };

    public void sendMessage(PushApi pushApi,String cid, String content, String title, String formId, String formType, String type, String status) {
        //根据cid进行单推
        PushDTO<Audience> pushDTO = new PushDTO<Audience>();
        // 设置推送参数
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        PushMessage pushMessage = new PushMessage();
        pushDTO.setPushMessage(pushMessage);
        GTNotification notification = new GTNotification();
        pushMessage.setNotification(notification);
        notification.setTitle(title);
        notification.setBody(content);
        notification.setClickType("payload");
//        notification.setUrl("");

        //获取已办任务id
        IdVo idVo = new IdVo();
        idVo.setId(formId);
        //String taskId = iActivitiService.getLastHiTaskId(idVo);

        //eaTask,单据涉及到的表名(如差旅费报销单ea_travel)
        Map<String,Object> taskParams = new HashMap<String,Object>();
        taskParams.put("form_id",formId);
        List<EaTask> eaTaskList  = eaTaskMapper.selectByMap(taskParams);
        EaTask eaTask = new EaTask();
        if(eaTaskList.size() > 0){
            eaTask = eaTaskList.get(0);
        }

        Map<String,Object> payload = new HashMap<String,Object>();
        payload.put("formId",formId);
        payload.put("formType",formType);
        payload.put("type",type);
        payload.put("status",status);
        payload.put("taskId",eaTask.getWorkflowTaskId());  //待办任务id
        notification.setPayload(JSON.toJSONString(payload));      //单据id,单据类型,类型(待办通知,已审批)
//        notification.setPayload("");

        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cid);                  //app的cid

        if(!EmptyUtil.checkNull(cid).equals("")){
//        audience.addAlias();                   //账号

            // 进行cid单推
            ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
            if (apiResult.isSuccess()) {
                // success
                System.out.println(apiResult.getData());
            } else {
                // failed
                System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
            }
        }
    }

}

